import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    //data
    // 每次页面刷新都会触发初始化
    userInfo: JSON.parse(localStorage.getItem("userInfo")),
    notifications: [],
    // dataSever: "http://172.21.212.103:8087/static/",
    // dataSever: "http://172.21.213.44:8087/static/",
    // dataSever: "http://118.195.175.4:8087/static/", // 可视化资源平台
    dataSever: "http://1.13.163.229:8087/static/",  // 参与式课程平台
    // dataSever: "http://192.168.0.44:8087/static/",
    // dataSever: "http://localhost:8097/static/",
  },
  getters: {
    userState: state => { 
      return state.userInfo.userState;
    },
    userName: state => {
      return state.userInfo.name;
    },
    userId: state => {
      return state.userInfo.userId;
    },
    avatar: state => {
      return state.userInfo.avatar;
    },
    userInfo: state => {
      return state.userInfo;
    },
    userEmail: state => {
      return state.userInfo.email;
    },
    notifications: state => {
      return state.notifications;
    },
    dataSever: state => {
      return state.dataSever;
    }
  },
  mutations: {
    getUserInfo: state => {
      if (!state.userInfo.userState) {
        $.ajax({
          url: "/GeoProblemSolving/user/state",
          type: "POST",
          async: false,
          success: function (data) {
            if (data) {
              let userInfo = {};
              userInfo = data;
              userInfo.userState = true;
              state.userInfo = userInfo;
            }
          },
          error: function (err) {
            console.log("Get user info fail.");
          }
        });
      }
    },
    userLogin: (state, data) => {
      let userInfo = data;
      userInfo.userState = true;
      state.userInfo = userInfo;
      localStorage.setItem("userInfo", JSON.stringify(state.userInfo));
    },
    userLogout: (state) => {
      state.userInfo = {
        userState: false,
        name: 'visitor',
        userId: '',
        avatar: '',
        token: '',
      };
      localStorage.setItem("userInfo", JSON.stringify(state.userInfo));
    },
    uploadAvatar: (state, avatar) => {
      state.userInfo.avatar = avatar;
    },
    setUserInfo: (state, userInfo) => {
      state.userInfo = userInfo;
    },
    updateProject: (state, projectId) => {
      //删除某项project
      let index = state.userInfo.createdProjects.indexOf(projectId);
      state.userInfo.createdProjects.splice(index, 1);
    },
    // setActivityTree: (state, activityTree) => {
    //     Vue.set(state, "activityTree", activityTree);
    // },
    clearTempOperations: (state) => {
      Vue.set(state, "tempOperationChange", []);
    },
    clearActivityTasks: (state) => {
      Vue.set(state, "activityTaskChange", []);
    },
    updateTempOperations: (state, data) => {
      state.tempOperationChange.push(data);
    },
    updateActivityTasks: (state, data) => {
      state.activityTaskChange.push(data);
    },
    addNotification: (state, notice) => {
      state.notifications.push(notice);
    },
    cleanNotification: (state) => {
      state.notifications = [];
    }
  }
})
